#include<iostream>
#include<vector>
using namespace std;


int jewelleryValue(vector<vector<int>>& frame) {
    int m = frame.size(), n = frame[0].size();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1));
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + frame[i-1][j-1];
        }
    }
    return dp[m][n];
}
int minFallingPathSum(vector<vector<int>>& matrix) {
    int m = matrix.size();
    vector<vector<int>> dp(m + 1, vector<int>(m + 1));
    for (int i = 0; i <= m; i++) dp[i][0] = INT_MAX;
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (j == m) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + matrix[i - 1][j - 1];
            else dp[i][j] = min({ dp[i - 1][j], dp[i - 1][j - 1], dp[i - 1][j + 1] }) + matrix[i - 1][j - 1];
        }
    }
    int min = INT_MAX;
    for (int i = 1; i <= m; i++)
        if (dp[m][i] < min) min = dp[m][i];
    return min;
}
int main()
{
    vector<vector<int>> vv{ {2,1,3} ,{6,5,4},{7,8,9} };
    //jewelleryValue(vv);
    minFallingPathSum(vv);
	return 0;
}